package com.pwc.conversionTool.controller;

import java.util.Arrays;
import java.util.Locale;

import javax.servlet.ServletContext;

import net.sf.saxon.functions.Replace;

import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pwc.conversionTool.IO.Reader;
import com.pwc.conversionTool.Manager.SoapUIManager;
import com.pwc.conversionTool.Manager.XMLConfigImpl;
import com.pwc.conversionTool.config.UploadFileView;

@Controller
public class RelationController {

	private static final Logger logger = LoggerFactory.getLogger(RelationController.class);

	@Autowired
	public ServletContext context;

	@RequestMapping(value = "/relation/tree", method = RequestMethod.GET)
	public String treeStructure(Locale locale, Model model) {

		return "relation/tree";
	}

	@RequestMapping(value = "/relation/createRelation", method = RequestMethod.GET)
	public  String CreateRelation(Locale locale, Model model) throws DocumentException {

		try {
			XMLConfigImpl xmlConfigImpl = XMLConfigImpl.initial(context);
			model.addAttribute("isInitial", SoapUIManager.isInitial());
			model.addAttribute("endpoint", xmlConfigImpl.getEndpoint());
			if (SoapUIManager.isInitial()) {
//				model.addAttribute("CsvTemplate", Reader.getAllFileFromStringPath(xmlConfigImpl.getCSVTemplatePath()));
				model.addAttribute("operationNames", SoapUIManager.getSoapUIClient().getOperationsName());
			}
		} catch (Exception e) {
			logger.error("log4j is running in createRelation");

			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return "relation/createRelation";
	}
	
	
	/**
	 * select relation
	 * 
	 * @param locale
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/relation/editRelation", method = RequestMethod.GET)
	public String EditRelation(Locale locale, Model model) {
		return "relation/editRelation";
	}

	/**
	 * setting filter of the csv file
	 * 
	 * @param path
	 * @param locale
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/relation/editRelationFilter", method = RequestMethod.POST)
	public String editRelationFilter(String path, Locale locale, Model model) {

		model.addAttribute("path", path);

		return "relation/editRelationFilter";
	}

	/**
	 * doing function
	 * 
	 * @param path
	 * @param locale
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/relation/editRelationFunction", method = RequestMethod.POST)
	public String editRelationFunction(String path, Locale locale, Model model) {
		model.addAttribute("path", path);

		return "relation/editRelationFunction";
	}

}
